package LinkedList.Practice;

public class MergeLinkedList {

    static class ListNode {
        int val;
        ListNode next;
        public ListNode () {

        }
        public ListNode (int val) {
            this.val = val;
        }
    }

    public static ListNode mergeLinkedList(ListNode headA,ListNode headB) {
        ListNode tempHead = new ListNode();
        ListNode newHead = tempHead;

        while(headA != null && headB != null) {
            if(headA.val > headB.val) {
                tempHead.next = headB;
                tempHead = tempHead.next;
                headB = headB.next;
            }else{
                tempHead.next = headA;
                tempHead = tempHead.next;
                headA = headA.next;
            }
        }

        if(headA != null) {
            tempHead.next = headA;
        }else {
            tempHead.next = headB;
        }
        return newHead.next;
    }

    public static void main(String[] args) {
        ListNode headA = new ListNode(12);
        ListNode node1 = new ListNode(23);
        ListNode node2 = new ListNode(34);
        headA.next = node1;
        node1.next = node2;
        ListNode headB = new ListNode(20);
        ListNode node3 = new ListNode(30);
        headB.next = node3;
        ListNode res = mergeLinkedList(headA,headB);
        System.out.println("aaa");
    }
}
